﻿// <copyright file="GameInformationImporter.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication.Import
{
    using System;

    internal class GameInformationImporter : VisualCollectionImporter<GameInformation>
    {
        private readonly int Visibility = 0;
        private readonly int CreationDate = 1;
        private readonly int Title = 2;
        private readonly int PublicInformation = 3;
        private readonly int GMInformation = 4;

        public GameInformationImporter(DataWorkspace workspace)
            : base(workspace)
        {
        }

        protected override bool SkipEntity(int index)
        {
            return false;
        }

        protected override int?[] MapHeaders(string[] stringHeaders)
        {
            int?[] headers = new int?[5];

            for (int i = 0; i < stringHeaders.Length; i++)
            {
                if (stringHeaders[i].StartsWith("Vis", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Visibility] = i;
                }
                else if (stringHeaders[i].StartsWith("Cre", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[CreationDate] = i;
                }
                else if (stringHeaders[i].StartsWith("Tit", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Title] = i;
                }
                else if (stringHeaders[i].StartsWith("Public", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[PublicInformation] = i;
                }
                else if (stringHeaders[i].StartsWith("GM", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[GMInformation] = i;
                }
            }

            return headers;
        }

        protected override void FillEntity(GameInformation entity, int index, int?[] headers, string[] data)
        {
            entity.CreationDate = DateTime.Now;

            if (headers[Visibility].HasValue)
            {
                int value;
                if (int.TryParse(data[index + headers[Visibility].Value], out value))
                {
                    entity.Visibility = value;
                }
            }

            if (headers[CreationDate].HasValue)
            {
                DateTime value;
                if (DateTime.TryParse(data[index + headers[CreationDate].Value], out value))
                {
                    entity.CreationDate = value;
                }
            }

            if (headers[Title].HasValue)
            {
                entity.Title = data[index + headers[Title].Value];
            }

            if (headers[PublicInformation].HasValue)
            {
                entity.PublicInformation = data[index + headers[PublicInformation].Value];
            }

            if (headers[GMInformation].HasValue)
            {
                entity.GMInformation = data[index + headers[GMInformation].Value];
            }
        }
    }
}
